package controllers

import (
	"encoding/json"
	"fmt"
	"github.com/360EntSecGroup-Skylar/excelize"
	"github.com/astaxie/beego"
	"github.com/nfnt/resize"
	"github.com/signintech/gopdf"
	"image/jpeg"
	"io"
	"io/ioutil"
	"log"
	"os"
	"strconv"
	"time"
	"yunshangxiaozhao/models"
	"yunshangxiaozhao/utils"
)

// Operations about Users
type UserController struct {
	beego.Controller
}

const (
	EntrepreneurialElite = 0
	ChineseStudent       = 1
	ForeignStudent       = 2
	Postgraduate         = 3
)

func (u *UserController) RetData(resp map[string]interface{}) {
	resp["code"] = 200
	u.Data["json"] = resp
	u.ServeJSON()
}

// @Title	RegisterUser
// @Description	register user
// @Param	username	body	string	true	"username for user"
// @Param	password	body	string	true 	"password for user"
// @Param	checkPassword	body	string	true	"password again"
// @Success 200	{int}	models.User.Id	register success
// @Failure 400 body is empty
// @router	/register [post]
func (u *UserController) Register() {
	resp := make(map[string]interface{})
	req := make(map[string]interface{})
	_ = json.Unmarshal(u.Ctx.Input.RequestBody, &req)

	password := req["password"].(string)
	checkPassword := req["checkPassword"].(string)
	if password != checkPassword {
		u.Abort("PasswordNotSame")
	}
	err := utils.CheckUserPassword(password)
	if err != nil {
		u.Abort(err.Error())
	}
	user := models.User{
		Username: req["username"].(string),
		Password: password,
	}

	dbUser, _ := models.QueryUserByUsername(req["username"].(string))
	if dbUser != nil && dbUser.Id > 0 {
		u.Abort("UsernameAlreadyExist")
	}

	userId, err := models.Register(&user)
	if err != nil {
		u.Abort("SystemFailed")
	} else {
		resp["msg"] = "注册成功"
		resp["UserId"] = userId
	}

	u.RetData(resp)
}

// @Title	Login
// @Description	Log user into the system
// @Param	username	body	string	true	"username for user"
// @Param	password	body	string 	true	"password for user"
// @Success 200 {int}	models.User.Id	login success
// @Failure 400 user not exist
// @router	/login [post]
func (u *UserController) Login() {
	resp := make(map[string]interface{})
	req := make(map[string]interface{})
	_ = json.Unmarshal(u.Ctx.Input.RequestBody, &req)
	user := models.User{
		Username: req["username"].(string),
		Password: req["password"].(string),
	}

	userId, err := models.Login(user)
	if err != nil {
		u.Abort(err.Error())
	} else {
		resp["msg"] = "登录成功"
		resp["UserId"] = userId
	}
	u.SetSession("userId", userId)

	u.RetData(resp)
}

// @Title	WxLogin
// @Description	Log wechat user into the system
// @Param	code	formData	string	"js_code for mini-program login"
// @Success 200 {int} models.User.Id	login success
// @Failure 400 user not exist
// @router /wx/login [post]
func (u *UserController) WxLogin() {
	resp := make(map[string]interface{})
	code := u.GetString("code")

	data, err := models.WxLogin(code)
	if err != nil {
		u.Abort(err.Error())
	}

	resp["msg"] = "登录成功"
	resp["data"] = data
	u.SetSession("userId", data["user_id"].(int))

	u.RetData(resp)
}

// @Title Query
// @Description 账号与安全接口(P65)
// @Success 200 {object} query success
// @Failure 400 user not exist
// @router /query [get]
func (u *UserController) Query() {
	resp := make(map[string]interface{})
	userId := u.getUserIdFromSession()

	user, err := models.QueryUserById(userId)
	if err != nil {
		u.Abort(err.Error())
	} else {
		resp["msg"] = "获取账号信息成功"
		data := make(map[string]interface{})
		data["Id"] = user.Id
		data["Telephone"] = user.Telephone
		resp["data"] = data
	}

	u.RetData(resp)
}

// TODO 以下 需要修改 error
// @Title QueryMoney
// @Description 获取金豆(P61)
// @Success 200 {int} query success
// @Failure 403 user not exist
// @router /money [get]
func (u *UserController) QueryMoney() {
	resp := make(map[string]interface{})
	userId := u.getUserIdFromSession()

	user, err := models.QueryUserById(userId)
	if err != nil {
		u.Abort("GetDataFail")
	} else {
		resp["msg"] = "查询用户金豆成功"
		data := make(map[string]interface{})
		data["Money"] = user.Money
		resp["data"] = data
	}

	u.RetData(resp)
}

// @Title UpdateTelephone
// @Description 用户申请更换手机号(P65)
// @Param	Telephone	body	string	true	"new telephone for user"
// @Success 200	update success
// @Failure 400	user not exist
// @router /tel [put]
func (u *UserController) UpdateTelephone() {
	var user models.User
	var dbUser *models.User
	resp := make(map[string]interface{})
	req := make(map[string]interface{})
	err := json.Unmarshal(u.Ctx.Input.RequestBody, &req)
	if err != nil {
		u.Abort("JsonToObjectFail")
	}
	tel := req["Telephone"].(string)
	userId := u.getUserIdFromSession()
	dbUser, _ = models.QueryUserById(userId)
	if tel == dbUser.Telephone {
		u.Abort("DataAlreadyExist")
	}

	user.Id = userId
	user.Telephone = tel
	err = models.ChangeTelephone(user)
	if err != nil {
		u.Abort("UpdateDataFail")
	} else {
		resp["msg"] = "修改手机号成功"
	}

	u.RetData(resp)
}

// @Title UpdatePassword
// @Description 用户申请更换密码(P65)
// @Param	oldPassword	body	string	true	"old password for user"
// @Param	password	body	string	true	"new password for user"
// @Param	checkPassword	body	string	true	"password again"
// @Success 200 query success
// @Failure 403 user not exist
// @router /pass [put]
func (u *UserController) UpdatePassword() {
	var user models.User
	resp := make(map[string]interface{})
	req := make(map[string]interface{})
	err := json.Unmarshal(u.Ctx.Input.RequestBody, &req)
	if err != nil {
		u.Abort("JsonToObjectFail")
	}

	oldPassword := req["oldPassword"].(string)
	password := req["password"].(string)
	checkPassword := req["checkPassword"].(string)
	if password != checkPassword {
		u.Abort("PasswordNotSame")
	}
	err = utils.CheckUserPassword(password)
	if err != nil {
		u.Abort(err.Error())
	}

	userId := u.getUserIdFromSession()
	dbUser, _ := models.QueryUserById(userId)
	if oldPassword != dbUser.Password {
		u.Abort("PasswordNotSame")
	}
	if password == dbUser.Password {
		u.Abort("DataAlreadyExist")
	}

	user.Id = userId
	user.Password = password
	err = models.ChangePassword(user)
	if err != nil {
		u.Abort("UpdateDataFail")
	} else {
		resp["msg"] = "修改密码成功"
	}

	u.RetData(resp)
}

// @Title UpdateEmail
// @Description 用户申请更换手机号(P65)
// @Param	Email	body	string	true	"new email for user"
// @Success 200	update success
// @Failure 400	user not exist
// @router /email [put]
func (u *UserController) UpdateEmail() {
	var user models.User
	var dbUser *models.User
	resp := make(map[string]interface{})
	req := make(map[string]interface{})
	err := json.Unmarshal(u.Ctx.Input.RequestBody, &req)
	if err != nil {
		u.Abort("JsonToObjectFail")
	}

	email := req["Email"].(string)
	userId := u.getUserIdFromSession()
	dbUser, _ = models.QueryUserById(userId)
	if email == dbUser.Email {
		u.Abort("DataAlreadyExist")
	}

	user.Id = userId
	user.Email = email
	err = models.ChangeEmail(user)
	if err != nil {
		u.Abort("UpdateDataFail")
	} else {
		resp["msg"] = "修改邮箱成功"
	}

	u.RetData(resp)
}

// @Title	QueryBrowseHistory
// @Description	浏览历史-谁看了我(P59)
// @Param	keyword	query	string	false	"是否查询关键字"
// @Param	page	query 	int	true	"The page number(the index start with 0)"
// @Param	limit	query 	int	true	"The limit number"
// @Success 200 {object} query success
// @Failure 400 user not login
// @router /browsed [get]
func (u *UserController) QueryBrowseHistory() {
	resp := make(map[string]interface{})
	userId := u.getUserIdFromSession()
	keyword := u.GetString("keyword")
	limit, page := u.getLimitAndPage()

	data, err := models.QueryBrowsedHistoryByUserId(userId, keyword, page, limit)
	if err != nil {
		u.Abort("GetDataFail")
	}

	resp["data"] = data
	resp["msg"] = "获取被浏览记录成功"
	u.RetData(resp)
}

// @Title QueryMyBrowseHistory
// @Description 浏览历史-我的浏览(P59)
// @Param	page	query 	int	true	"The page number(the index start with 0)"
// @Param	limit	query 	int	true	"The limit number"
// @Success 200 {object} query success
// @Failure 403 user not login
// @router /browsed [get]
func (u *UserController) QueryMyBrowseHistory() {
	resp := make(map[string]interface{})
	userId := u.getUserIdFromSession()
	keyword := u.GetString("keyword")
	limit, page := u.getLimitAndPage()

	data, err := models.QueryBrowseHistoryByUserId(userId, keyword, page, limit)
	if err != nil {
		u.Abort("GetDataFail")
	}
	resp["data"] = data
	resp["msg"] = "获取我的浏览记录成功"

	u.RetData(resp)
}

// @Title AddUserBrowseHistory
// @Description 记录浏览记录(用户每次点击公司详情时记录)
// @Param	CompanyId	form	int	true	"被浏览的公司"
// @Success 200 {object} query success
// @Failure 400 user not login
// @router /browse [post]
func (u *UserController) AddUserBrowseHistory() {
	var browse models.UserCompanyBrowse
	resp := make(map[string]interface{})
	companyId, err := u.GetInt("CompanyId")
	if err != nil {
		u.Abort("JsonToObject")
	}
	userId := u.getUserIdFromSession()

	browse.User, _ = models.QueryUserById(userId)
	browse.Company, _ = models.GetCompanyById(companyId)
	browse.Type = 0

	err = models.AddBrowseCompany(&browse)
	if err != nil {
		u.Abort("AddDataFail")
	} else {
		resp["msg"] = "记录成功"
	}

	u.RetData(resp)
}

// @Title	UpdateUserInfo
// @Description	用户修改个人信息(P60)
// @Param	Nickname	formData	string	true	"昵称"
// @Param	RealName	formData	string	true	"真实姓名"
// @Param	Gender	formData	int	true	"性别(0-保密 1-男 2-女)"
// @Param	University	formData	string	true	"学校名称"
// @Param	College	formData	string	true	"院系名称"
// @Param	Major	formData	string	true	"专业名称"
// @Param	Grade	formData	int	true	"年级"
// @Param	Position	formData	string	true	"职务"
// @Param	Department	formData	string	true	"任职部门"
// @Param	City	formData	string	true	"城市"
// @Param	Area	formData	string	true	"区域"
// @Param	Province	formData	string	true	"省"
// @Param	Avatar	formData	string	true	"头像地址"
// @Success 200 query success
// @Failure 400 user not login
// @router /info [put]
func (u *UserController) UpdateUserInfo() {
	var user models.User
	resp := make(map[string]interface{})
	if err := u.ParseForm(&user); err != nil {
		u.Abort("JsonToObjectFail")
	}
	userId := u.getUserIdFromSession()
	user.Id = userId

	err := models.UpdateUserInfo(user)
	if err != nil {
		u.Abort("UpdateDataFail")
	} else {
		resp["msg"] = "修改用户信息成功"
	}

	u.RetData(resp)
}

// @Title	UpdateUserRealPhoto
// @Description	用户修改个人信息(P60)
// @Param RealPhoto	formData string	true	"一寸照存放地址"
// @Success 200 query success
// @Failure 400 user not login
// @router /real_photo [put]
func (u *UserController) UpdateUserRealPhoto() {
	var user models.User
	resp := make(map[string]interface{})
	if err := u.ParseForm(&user); err != nil {
		u.Abort("JsonToObjectFail")
	}
	userId := u.getUserIdFromSession()
	user.Id = userId

	err := models.UpdateUserRealPhoto(user)
	if err != nil {
		u.Abort("UpdateDataFail")
	} else {
		resp["msg"] = "修改用户一寸照成功"
	}

	u.RetData(resp)
}

// @Title QueryPutResume
// @Description 已投简历(P14)
// @Param	result	query	int	true	"可不传, 不传则返回所有已投简历 The result (1-已投 2-邀请一面 3-邀请二面 4-接受offer)"
// @Param	page	query 	int	true	"The page number(the index start with 0)"
// @Param	limit	query 	int	true	"The limit number"
// @Success 200 {object} query success
// @Failure 400 user not login
// @router /resume/put [get]
func (u *UserController) QueryPutResume() {
	resp := make(map[string]interface{})
	var data []*models.UserResumePut
	limit, page := u.getLimitAndPage()
	userId := u.getUserIdFromSession()
	result, err := u.GetInt("result")
	if err != nil {
		data, err = models.QueryPutResumeById(userId, page, limit)
	} else {
		data, err = models.QueryPutResumeByResult(userId, result, page, limit)
	}

	if err != nil {
		u.Abort("GetDataFail")
	}
	resp["data"] = data
	resp["msg"] = "获取已投简历成功"

	u.RetData(resp)
}

// @Title QueryPutResumeByCompany
// @Description 已投简历
// @Param	companyId	query	int	true	"根据指定公司查看已投简历"
// @Param	page	query 	int	true	"The page number(the index start with 0)"
// @Param	limit	query 	int	true	"The limit number"
// @Success 200 {object} query success
// @Failure 400 user not login
// @router /resume/put/company [get]
func (u *UserController) QueryPutResumeByCompany() {
	resp := make(map[string]interface{})
	var data []*models.UserResumePut
	limit, page := u.getLimitAndPage()
	userId := u.getUserIdFromSession()
	companyId, err := u.GetInt("companyId")
	data, err = models.QueryPutResumeByCompany(userId, companyId, page, limit)

	if err != nil {
		u.Abort("GetDataFail")
	}
	resp["data"] = data
	resp["msg"] = "获取指定公司的已投简历成功"

	u.RetData(resp)
}

// @Title PutResume
// @Description 添加简历
// @Param	ResumeId	body	int	true	"resume to put"
// @Param	CompanyId	body	int	true	"company to put"
// @Param	JobId	body	int	true	"job that you want to put resume"
// @Success 200 {object} query success
// @Failure 400 user not login
// @router /resume/put [post]
func (u *UserController) PutResume() {
	var resumePut models.UserResumePut
	resp := make(map[string]interface{})
	req := make(map[string]interface{})
	err := json.Unmarshal(u.Ctx.Input.RequestBody, &req)
	if err != nil {
		u.Abort("JsonToObjectFail")
	}
	resumeId := int(req["ResumeId"].(float64))
	companyId := int(req["CompanyId"].(float64))
	jobId := int(req["JobId"].(float64))
	resumePut.Resume, err = models.QueryResumeByResumeId(resumeId)
	resumePut.Company, err = models.GetCompanyById(companyId)
	resumePut.Job, err = models.GetCompanyJobById(jobId)
	userId := u.getUserIdFromSession()
	user, _ := models.QueryUserById(userId)
	resumePut.User = user

	resumePutId, err := models.PutResume(resumePut)
	if err != nil {
		u.Abort("AddDataFail")
	}

	resp["ResumePutId"] = resumePutId
	resp["msg"] = "投简历成功"

	u.RetData(resp)
}

// @Title	UpdateResumePut
// @Description	公司接受用户简历
// @Param	Id	body	int	true	"user put resume id"
// @Param	Result	body	int	true	"status company want resume to enter(2-接受一面 3-接受二面 4-提供offer)"
// @Param	ResultType	body	int	true	"结果类型(0-拒绝 1-同意)"
// @Success 200	update success
// @Failure 400	user not login
// @router /resume/put [put]
func (u *UserController) UpdateResumePut() {
	var userResumePut models.UserResumePut
	resp := make(map[string]interface{})
	if err := u.ParseForm(&userResumePut); err != nil {
		u.Abort("JsonToObjectFail")
	}

	err := models.UpdateResumePut(userResumePut)
	if err != nil {
		u.Abort("UpdateDataFail")
	} else {
		resp["msg"] = "更新用户简历状态成功"
	}

	u.RetData(resp)
}

// @Title QueryUserInfo
// @Description 查看个人信息(我的名片(P60), 可查看他人信息)
// @Param	id	query	int	false	"when view other user's info, please add id param to url"
// @Success 200 {object} query success
// @Failure 400 user not login
// @router /info [get]
func (u *UserController) QueryUserInfo() {
	resp := make(map[string]interface{})
	var user *models.User
	id, err := u.GetInt("id")
	userId := u.getUserIdFromSession()

	if err != nil || id == userId {
		user, err = models.QueryUserById(userId)
		if err != nil {
			u.Abort("GetDataFail")
		}
	} else {
		user, err = models.QueryUserById(id)
		if err != nil {
			u.Abort("GetDataFail")
		}
	}
	resp["data"] = user
	resp["msg"] = "获取用户信息成功"

	u.RetData(resp)
}

// @Title AddComment
// @Description 添加评论/投诉(P63)
// @Param	content	body	string	true	"content for user comment"
// @Param	importance	body	int	false	"评论可不传 important level for this comment(0-一般 1-重要 2-非常重要)"
// @Param	urge	body	int	false	"评论可不传 urge level for this comment(0-一般 1-紧急 2-非常紧急)"
// @Success 200 {object} query success
// @Failure 400 user not login
// @router /comment [post]
func (u *UserController) AddComment() {
	var comment models.UserComment
	resp := make(map[string]interface{})
	err := json.Unmarshal(u.Ctx.Input.RequestBody, &comment)
	if err != nil {
		u.Abort("JsonToObjectFail")
	}
	userId := u.getUserIdFromSession()

	user, _ := models.QueryUserById(userId)
	comment.CommentUser = user

	err = models.AddComment(&comment)
	if err != nil {
		u.Abort("AddDataFail")
	} else {
		resp["msg"] = "评论成功"
	}

	u.RetData(resp)
}

// @Title QueryUserComment
// @Description 查看我的投诉/建议/意见
// @Param	page	query 	int	true	"The page number(the index start with 0)"
// @Param	limit	query 	int	true	"The limit number"
// @Success 200 {object} query success
// @Failure 403 user not exist
// @router /comment [get]
func (u *UserController) QueryUserComment() {
	resp := make(map[string]interface{})
	userId := u.getUserIdFromSession()
	limit, page := u.getLimitAndPage()

	userComment, err := models.QueryCommentById(userId, page, limit)
	if err != nil {
		u.Abort("GetDataFail")
	}
	resp["data"] = userComment
	resp["msg"] = "获取用户投诉信息成功"

	u.RetData(resp)
}

// @Title DeleteUserComment
// @Description 取消我的投诉/建议/意见
// @Param	Id	body	int 	true	"comment id for user comment"
// @Success 200 delete success
// @Failure 400 user not login
// @router /comment [delete]
func (u *UserController) DeleteUserComment() {
	var comment *models.UserComment
	resp := make(map[string]interface{})
	err := json.Unmarshal(u.Ctx.Input.RequestBody, &comment)
	if err != nil {
		u.Abort("JsonToObjectFail")
	}
	userId := u.getUserIdFromSession()

	user, _ := models.QueryUserById(userId)
	comment.CommentUser = user

	err = models.DeleteComment(comment)
	if err != nil {
		u.Abort("DeleteDataFail")
	}
	resp["msg"] = "删除用户投诉信息成功"

	u.RetData(resp)
}

// @Title AddLabel
// @Description	添加标签
// @Param	Label	body	string	true	"label content"
// @Param	Type	body	int	true	"label type(0-形象性格 1-核心能力 2-综合特长)"
// @Success 200 {object} query success
// @Failure 400 user not login
// @router /label [post]
func (u *UserController) AddLabel() {
	var label models.UserLabel
	resp := make(map[string]interface{})
	err := json.Unmarshal(u.Ctx.Input.RequestBody, &label)
	if err != nil {
		u.Abort("JsonToObjectFail")
	}
	userId := u.getUserIdFromSession()

	user, _ := models.QueryUserById(userId)
	label.User = user

	err = models.AddLabel(&label)
	if err != nil {
		u.Abort(err.Error())
	} else {
		resp["msg"] = "添加标签成功"
	}

	u.RetData(resp)
}

// @Title DeleteUserLabel
// @Description 删除我的标签
// @Param	Id	body	int	true	"label id for user label"
// @Success 200 delete success
// @Failure 400 user not login
// @router /label [delete]
func (u *UserController) DeleteUserLabel() {
	var label *models.UserLabel
	resp := make(map[string]interface{})
	err := json.Unmarshal(u.Ctx.Input.RequestBody, &label)
	if err != nil {
		u.Abort("JsonToObjectFail")
	}
	userId := u.getUserIdFromSession()

	user, _ := models.QueryUserById(userId)
	label.User = user

	err = models.DeleteLabel(label)
	if err != nil {
		u.Abort("DeleteDataFail")
	}
	resp["msg"] = "删除标签成功"

	u.RetData(resp)
}

// @Title QueryUserLabel
// @Description 查看我的标签
// @Param	id	query	int	false	"when view other user's labels, please add id param to url"
// @Success 200 {object} query success
// @Failure 400 user not login
// @router /label [get]
func (u *UserController) QueryUserLabel() {
	resp := make(map[string]interface{})
	userId := u.getUserIdFromSession()
	id, err := u.GetInt("id")
	var userLabels []models.MyLabel

	if err != nil || id == userId {
		userLabels, err = models.QueryUserLabel(userId)
	} else {
		userLabels, err = models.QueryOtherLabel(id, userId)
	}

	if err != nil {
		u.Abort("GetDataFail")
	}

	resp["data"] = userLabels
	resp["msg"] = "获取标签信息成功"

	u.RetData(resp)
}

// @Title PraiseLabel
// @Description 点赞标签
// @Param	LabelId	body	int	true	"label id for user"
// @Param	PraisedUser	body	int	true	"praised user id"
// @Success 200 {object} query success
// @Failure 400 user not login
// @router /praise [post]
func (u *UserController) PraiseLabel() {
	var praise models.UserPraise
	req := make(map[string]interface{})
	resp := make(map[string]interface{})
	err := json.Unmarshal(u.Ctx.Input.RequestBody, &req)
	if err != nil {
		u.Abort("JsonToObjectFail")
	}

	labelId := int(req["LabelId"].(float64))
	praise.UserLabel, err = models.QueryLabel(labelId)
	userId := u.getUserIdFromSession()
	user, _ := models.QueryUserById(userId)
	praise.PraiseUser = user

	err = models.PraiseLabel(&praise)
	if err != nil {
		u.Abort("AddDataFail")
	} else {
		resp["msg"] = "点赞标签成功"
	}

	u.RetData(resp)
}

// @Title CancelPraise
// @Description 取消我的对标签的点赞
// @Param	LabelId	body	int 	true	"praise id for user comment"
// @Success 200 delete success
// @Failure 400 user not login
// @router /praise [delete]
func (u *UserController) CancelPraise() {
	var praise models.UserPraise
	resp := make(map[string]interface{})
	req := make(map[string]interface{})
	err := json.Unmarshal(u.Ctx.Input.RequestBody, &req)
	if err != nil {
		u.Abort("JsonToObjectFail")
	}
	praise.UserLabel = &models.UserLabel{Id: int(req["LabelId"].(float64))}
	userId := u.getUserIdFromSession()
	user, _ := models.QueryUserById(userId)
	praise.PraiseUser = user

	err = models.CancelPraise(praise)
	if err != nil {
		u.Abort("DeleteDataFail")
	}
	resp["msg"] = "取消用户点赞信息成功"

	u.RetData(resp)
}

// @Title AddUserHonor
// @Description 添加荣誉
// @Param	Description	formData	string	true	"description for user honor"
// @Param	Type	formData	int	true	"type for user honor(0-奖学金 1-聘任证书 2-赛事获奖 3-科研成果 4-其他证书)"
// @Param	GetTimeYear	formData	int	true	"get time year for this honor"
// @Param	GetTimeMonth	formData	int	true	"get time month for this honor"
// @Param	Img	formData	string	true	"img path for user honor"
// @Param	Level	formData	string	true	"奖学金等级(1-校级 2-省级 3-国家级) / 专利号 / 论文名称"
// @Success 200 {int} add success
// @Failure 400 user not login
// @router /honor [post]
func (u *UserController) AddUserHonor() {
	var honor models.UserHonor
	resp := make(map[string]interface{})
	honor.Description = u.GetString("Description")
	honor.Type, _ = u.GetInt("Type")
	honor.GetTimeYear, _ = u.GetInt("GetTimeYear")
	honor.GetTimeMonth, _ = u.GetInt("GetTimeMonth")
	honor.Img = u.GetString("Img")
	honor.Level = u.GetString("Level")
	userId := u.getUserIdFromSession()
	user, _ := models.QueryUserById(userId)
	honor.User = user

	id, err := models.AddHonor(&honor)
	if err != nil {
		u.Abort("AddDataFail")
	} else {
		resp["msg"] = "添加荣誉成功"
		resp["HonorId"] = id
	}

	u.RetData(resp)
}

// @Title QueryHonor
// @Description 查看我的荣誉
// @Param	id	query	int	false	"when view other user's honor, please add id param to url"
// @Param	page	query 	int	true	"The page number(the index start with 0)"
// @Param	limit	query 	int	true	"The limit number"
// @Success 200 {object} query success
// @Failure 400 user not login
// @router /honor [get]
func (u *UserController) QueryHonor() {
	resp := make(map[string]interface{})
	id, err := u.GetInt("id")
	userId := u.getUserIdFromSession()
	limit, page := u.getLimitAndPage()
	var userHonors []*models.UserHonor

	if id == userId || err != nil {
		userHonors, err = models.QueryUserHonor(userId, page, limit)
	} else {
		userHonors, err = models.QueryUserHonor(id, page, limit)
	}

	if err != nil {
		u.Abort("GetDataFail")
	}

	resp["data"] = userHonors
	resp["msg"] = "获取荣誉信息成功"

	u.RetData(resp)
}

// @Title QueryOneHonor
// @Description 查看我的荣誉
// @Param	id path	int true "查看指定honorId的荣誉"
// @Success 200 {object} query success
// @Failure 400 user not login
// @router /honor/:id [get]
func (u *UserController) QueryOneHonor() {
	resp := make(map[string]interface{})
	id, err := u.GetInt(":id")

	userHonor, err := models.QueryHonor(id)

	if err != nil {
		u.Abort("GetDataFail")
	}

	resp["data"] = userHonor
	resp["msg"] = "获取荣誉信息成功"

	u.RetData(resp)
}

// @Title UpdateMyHonor
// @Description 修改我的荣誉
// @Param Id body	int true "指定honorId"
// @Param Description body	string true "更新的获奖描述"
// @Param Img body	string true "更新的获奖照片"
// @Param GetTimeYear body	int true "更新的获奖时间-年份"
// @Param GetTimeMonth body	int true "更新的获奖时间-月份"
// @Param Level body	string true "更新的获奖档次"
// @Param Type body	int true "更新的获奖类型"
// @Success 200 {object} query success
// @Failure 400 user not login
// @router /honor [put]
func (u *UserController) UpdateMyHonor() {
	var honor *models.UserHonor
	resp := make(map[string]interface{})
	err := json.Unmarshal(u.Ctx.Input.RequestBody, &honor)

	err = models.UpdateHonor(honor)
	if err != nil {
		u.Abort("GetDataFail")
	}

	resp["msg"] = "更新荣誉信息成功"

	u.RetData(resp)
}

// @Title DeleteUserHonor
// @Description 删除我的荣誉
// @Param	Id	body	int	true	"honor id of user honor"
// @Success 200 delete success
// @Failure 400 user not exist
// @router /honor [delete]
func (u *UserController) DeleteUserHonor() {
	var honor models.UserHonor
	resp := make(map[string]interface{})
	err := json.Unmarshal(u.Ctx.Input.RequestBody, &honor)
	if err != nil {
		u.Abort("JsonToObjectFail")
	}
	userId := u.getUserIdFromSession()

	user, _ := models.QueryUserById(userId)
	honor.User = user

	err = models.DeleteHonor(&honor)
	if err != nil {
		u.Abort("DeleteDataFail")
	}
	resp["msg"] = "删除荣誉成功"

	u.RetData(resp)
}

// @Title AddStar
// @Description 添加关注
// @Param	UserId	formData	int	true	"id of user who is stared"
// @Success 200 {int} StarId	add success
// @Failure 400 user not login
// @router /star [post]
func (u *UserController) AddStar() {
	var star models.UserStar
	resp := make(map[string]interface{})
	starUser, err := u.GetInt("UserId")
	if err != nil {
		u.Abort("GetDataFail")
	}
	star.StarUser, _ = models.QueryUserById(starUser)

	userId := u.getUserIdFromSession()

	star.User, _ = models.QueryUserById(userId)

	id, err := models.AddStar(&star)
	if err != nil {
		u.Abort("AddDataFail")
	} else {
		resp["msg"] = "关注成功"
		resp["StarId"] = id
	}

	u.RetData(resp)
}

// @Title QueryMyStar
// @Description 查看我的关注
// @Param	page	query 	int	true	"The page number(the index start with 0)"
// @Param	limit	query 	int	true	"The limit number"
// @Param	keyword	query	string	false	"搜索的关键字"
// @Success 200 {object} query success
// @Failure 403 user not exist
// @router /star [get]
func (u *UserController) QueryMyStar() {
	resp := make(map[string]interface{})
	userId := u.getUserIdFromSession()
	limit, page := u.getLimitAndPage()
	keyword := u.GetString("keyword")

	users, err := models.QueryMyStar(userId, page, limit, keyword)
	if err != nil {
		u.Abort("GetDataFail")
	}

	resp["data"] = users
	resp["msg"] = "获取我关注的人成功"

	u.RetData(resp)
}

// @Title DeleteUserStar
// @Description 取消关注
// @Param	Id	body	int	true	"user star id"
// @Success 200 delete success
// @Failure 400 user not exist
// @router /star [delete]
func (u *UserController) DeleteUserStar() {
	var userStar *models.UserStar
	resp := make(map[string]interface{})
	err := json.Unmarshal(u.Ctx.Input.RequestBody, &userStar)
	if err != nil {
		u.Abort("JsonToObjectFail")
	}
	userId := u.getUserIdFromSession()

	user, _ := models.QueryUserById(userId)
	userStar.User = user

	err = models.DeleteMyStar(userStar)
	if err != nil {
		u.Abort("DeleteDataFail")
	}
	resp["msg"] = "取消关注成功"

	u.RetData(resp)
}

// @Title	QueryUserIdentity
// @Description	查看指定类别的大学生俱乐部
// @Param	IdentityId	path	int	true	"identity id(创业精英-0, 中国籍留学生-1, 外国际留学生-2, 考研族-3)"
// @Param	page	query 	int	true	"The page number(the index start with 0)"
// @Param	limit	query 	int	true	"The limit number"
// @Success 200 {object} query success
// @Failure 403 user not exist
// @router /identity [get]
func (u *UserController) QueryUserIdentity() {
	resp := make(map[string]interface{})
	identityId, err := u.GetInt("IdentityId")
	if err != nil {
		u.Abort("JsonToObjectFail")
	}

	userId := u.getUserIdFromSession()
	limit, page := u.getLimitAndPage()

	users, err := models.QueryByIdentity(identityId, userId, page, limit)
	if err != nil {
		u.Abort("GetDataFail")
	}

	resp["data"] = users
	resp["msg"] = "获取指定大学生俱乐部成员信息成功"

	u.RetData(resp)
}

// @Title AddIdentity
// @Description 成为指定身份(0-创业精英 1-中国籍留学生 2-外国籍留学生 3-考研族)
// @Param	IdentityId	formData	int	true	"identity id for user identity that you want to become"
// @Success 200 {int} add success
// @Failure 400 user not login
// @router /identity [post]
func (u *UserController) AddIdentity() {
	var userIdentity models.UserIdentity
	resp := make(map[string]interface{})
	identityId, err := u.GetInt("IdentityId")
	if err != nil {
		u.Abort("GetDataFail")
	}
	userId := u.getUserIdFromSession()
	userIdentity.User, _ = models.QueryUserById(userId)

	// TODO 创业精英验证
	if identityId == EntrepreneurialElite {
		fmt.Println("成为创业精英验证!!!")
	}
	userIdentity.IdentityId = identityId

	id, err := models.InsertIdentity(&userIdentity)
	if err != nil {
		u.Abort("AddDataFail")
	} else {
		resp["msg"] = "申请成功"
		resp["UserIdentityId"] = id
	}

	u.RetData(resp)
}

// @Title DeleteUserIdentity
// @Description 申请取消用户身份
// @Param	Id	body	int	true	"user identity id"
// @Success 200 delete success
// @Failure 400 user not exist
// @router /identity [delete]
func (u *UserController) DeleteUserIdentity() {
	var userIdentity *models.UserIdentity
	resp := make(map[string]interface{})
	err := json.Unmarshal(u.Ctx.Input.RequestBody, &userIdentity)
	if err != nil {
		u.Abort("JsonToObjectFail")
	}
	userId := u.getUserIdFromSession()

	user, _ := models.QueryUserById(userId)
	userIdentity.User = user

	err = models.ExitIdentity(userIdentity)
	if err != nil {
		u.Abort("DeleteDataFail")
	}
	resp["msg"] = "申请取消身份成功"

	u.RetData(resp)
}

// @Title QueryBiStar
// @Description 查看互相关注的用户
// @Param	page	query 	int	true	"The page number(the index start with 0)"
// @Param	limit	query 	int	true	"The limit number"
// @Param	keyword	query	string	false	"搜索的关键字"
// @Success 200 {object} query success
// @Failure 403 user not exist
// @router /bi-star [get]
func (u *UserController) QueryBiStar() {
	resp := make(map[string]interface{})
	userId := u.getUserIdFromSession()
	limit, page := u.getLimitAndPage()
	keyword := u.GetString("keyword")

	users, err := models.QueryBiStar(userId, page, limit, keyword)
	if err != nil {
		u.Abort(err.Error())
	}

	resp["data"] = users
	resp["msg"] = "获取互相关注成功"

	u.RetData(resp)
}

// @Title QueryStarMe
// @Description 查看关注我的用户
// @Param	page	query 	int	true	"The page number(the index start with 0)"
// @Param	limit	query 	int	true	"The limit number"
// @Param	keyword	query	string	false	"搜索的关键字"
// @Success 200 {object} query success
// @Failure 403 user not exist
// @router /star-me [get]
func (u *UserController) QueryStarMe() {
	resp := make(map[string]interface{})
	userId := u.getUserIdFromSession()
	limit, page := u.getLimitAndPage()
	keyword := u.GetString("keyword")

	users, err := models.QueryStarMe(userId, page, limit, keyword)
	if err != nil {
		u.Abort(err.Error())
	}

	resp["data"] = users
	resp["msg"] = "获取关注我的用户成功"

	u.RetData(resp)
}

// @Title AddResume
// @Description 添加简历
// @Param	Area	formData	string	false	"area of user"
// @Param	Birth	formData	date	false	"birth of user"
// @Param	City	formData	string	false	"city of user"
// @Param	Computer	formData	int	false	"computer level of user"
// @Param	Department	formData	string false	"希望任职部门"
// @Param	Education 	formData	int	false	"教育程度(0-专科 1-本科 2-硕士 3-博士 4-博士后)"
// @Param	EducationType	formData	int	false	"教育类型(0-全日制 1-非全日制)"
// @Param	Email	formData	string	false	"电子邮箱"
// @Param	Faculty	formData	string	false	"所在院系"
// @Param	ForeignLanguage	formData	string	false	"外语语种"
// @Param	ForeignLanguageLevel	formData  string	false	"外语程度"
// @Param	Gender	formData	int	false	"性别(0-保密 1-男 2-女)"
// @Param	Height	formData	float	false	"身高(例: 1.80)"
// @Param	Major	formData	string	false	"主修专业"
// @Param	Minor	formData	string	false	"辅修专业"
// @Param	Name	formData	string	true	"姓名"
// @Param	Nationality	formData	string	false	"国籍"
// @Param	OtherComputerInformation	formData	string	false	"其他计算机信息说明"
// @Param	OtherForeignLanguageInformation	formData	string	false	"其他外语说明"
// @Param	Phone	formData	string	false	"电话号码"
// @Param	PoliticalStatus	formData	int	false	"政治地位(0-群众 1-共青团员 2-中共党员)"
// @Param	Position	formData	string	false	"最高职位"
// @Param	Province	formData	string	false	"地址-省份"
// @Param	SchoolEnd	formData	int	false	"入学时间-结束"
// @Param	SchoolName	formData	string	false	"学校名字"
// @Param	SchoolNationality	formData	string	false	"学校国籍"
// @Param	SchoolStart	formData	int	false	"入学时间-开始"
// @Param	SchoolType	formData	int	false	"学校类别(0-985 1-211 2-双一流 3-普通本科 4-专科)"
// @Param	SecondEducation	formData	string	false	"次高学历"
// @Param	SelfEvaluation	formData	string	false	"自我评价"
// @Param	Weight	formData	float	false	"体重(例: 65.12)"
// @Param	WorkingTimeEndMonth	formData	int	false	"任职时间-结束月"
// @Param	WorkingTimeEndYear	formData	int	false	"任职时间-结束年"
// @Param	WorkingTimeStartMonth	formData	int	false	"任职时间-开始月"
// @Param	WorkingTimeStartYear	formData	int	false	"任职时间-开始年"
// @Success 200 {int} add success
// @Failure 400 user not login
// @router /resume [post]
func (u *UserController) AddResume() {
	var userResume models.UserResume
	resp := make(map[string]interface{})
	if err := u.ParseForm(&userResume); err != nil {
		u.Abort("JsonToObjectFail")
	}
	userId := u.getUserIdFromSession()
	userResume.User, _ = models.QueryUserById(userId)

	id, err := models.AddResume(&userResume)
	if err != nil {
		u.Abort("AddDataFail")
	} else {
		resp["msg"] = "简历生成成功"
		resp["UserResumeId"] = id
	}

	u.RetData(resp)
}

// @Title QueryMyResumeList
// @Description 查看我的简历
// @Success 200 {object} query success
// @Failure 400 user not login
// @router /resume/list [get]
func (u *UserController) QueryMyResumeList() {
	resp := make(map[string]interface{})
	userId := u.getUserIdFromSession()

	resume, err := models.QueryResumeByUserId(userId)
	if err != nil {
		u.Abort("GetDataFail")
	}

	resp["data"] = resume
	resp["msg"] = "获取我的简历成功"

	u.RetData(resp)
}

// @Title QueryMyResume
// @Description 查看我的个人简历
// @Param resumeId int true "简历id"
// @Success 200 {object} query success
// @Failure 400 user not login
// @router /resume [get]
func (u *UserController) QueryMyResume() {
	resp := make(map[string]interface{})
	userId := u.getUserIdFromSession()

	resume, err := models.QueryResumeByResumeId(userId)
	if err != nil {
		u.Abort("GetDataFail")
	}

	resp["data"] = resume
	resp["msg"] = "获取我的简历成功"

	u.RetData(resp)
}

// @Title UpdateResume
// @Description 用户更改简历信息
// @Param	Area	formData	string	false	"area of user"
// @Param	Birth	formData	date	false	"birth of user"
// @Param	City	formData	string	false	"city of user"
// @Param	Computer	formData	int	false	"computer level of user"
// @Param	Department	formData	string false	"希望任职部门"
// @Param	Education 	formData	int	false	"教育程度(0-专科 1-本科 2-硕士 3-博士 4-博士后)"
// @Param	EducationType	formData	int	false	"教育类型(0-全日制 1-非全日制)"
// @Param	Email	formData	string	false	"电子邮箱"
// @Param	Faculty	formData	string	false	"所在院系"
// @Param	ForeignLanguage	formData	string	false	"外语语种"
// @Param	ForeignLanguageLevel	formData  string	false	"外语程度"
// @Param	Gender	formData	int	false	"性别(0-保密 1-男 2-女)"
// @Param	Height	formData	float	false	"身高(例: 1.80)"
// @Param	Major	formData	string	false	"主修专业"
// @Param	Minor	formData	string	false	"辅修专业"
// @Param	Name	formData	string	true	"姓名"
// @Param	Nationality	formData	string	false	"国籍"
// @Param	OtherComputerInformation	formData	string	false	"其他计算机信息说明"
// @Param	OtherForeignLanguageInformation	formData	string	false	"其他外语说明"
// @Param	Phone	formData	string	false	"电话号码"
// @Param	PoliticalStatus	formData	int	false	"政治地位(0-群众 1-共青团员 2-中共党员)"
// @Param	Position	formData	string	false	"最高职位"
// @Param	Province	formData	string	false	"地址-省份"
// @Param	SchoolEnd	formData	int	false	"入学时间-结束"
// @Param	SchoolName	formData	string	false	"学校名字"
// @Param	SchoolNationality	formData	string	false	"学校国籍"
// @Param	SchoolStart	formData	int	false	"入学时间-开始"
// @Param	SchoolType	formData	int	false	"学校类别(0-985 1-211 2-双一流 3-普通本科 4-专科)"
// @Param	SecondEducation	formData	string	false	"次高学历"
// @Param	SelfEvaluation	formData	string	false	"自我评价"
// @Param	Weight	formData	float	false	"体重(例: 65.12)"
// @Param	WorkingTimeEndMonth	formData	int	false	"任职时间-结束月"
// @Param	WorkingTimeEndYear	formData	int	false	"任职时间-结束年"
// @Param	WorkingTimeStartMonth	formData	int	false	"任职时间-开始月"
// @Param	WorkingTimeStartYear	formData	int	false	"任职时间-开始年"
// @Success 200 query success
// @Failure 400 user not login
// @router /resume [put]
func (u *UserController) UpdateResume() {
	var userResume models.UserResume
	resp := make(map[string]interface{})
	err := u.ParseForm(&userResume)
	if err != nil {
		u.Abort("JsonToObjectFail")
	}

	userId := u.getUserIdFromSession()
	userResume.User, err = models.QueryUserById(userId)
	if err != nil {
		u.Abort("GetDataFail")
	}

	err = models.UpdateResume(&userResume)
	if err != nil {
		u.Abort("UpdateDataFail")
	} else {
		resp["msg"] = "修改简历信息成功"
	}

	u.RetData(resp)
}

// @Title	UploadImg
// @Description	上传图片
// @Param	img	formData	file	true	"img stream to upload"
// @Param	type	formData	string	true	"which type to upload(honor / avatar / introduction / activity / realPhoto / company)"
// @Success 200	{object} upload success
// @Failure	400	data not found
// @router	/img [post]
func (u *UserController) UploadImg() {
	resp := make(map[string]interface{})
	path := u.GetString("type")
	f, _, err := u.GetFile("img")
	if err != nil {
		u.Abort("GetDataFail")
	} else {
		Img := "data/" + path + "/" + utils.GenerateRandomFileName()
		err = u.SaveToFile("img", Img)
		if err != nil {
			u.Abort("GetDataFail")
		}
		_ = f.Close()

		resp["data"] = Img
	}

	u.RetData(resp)
}

// @Title QueryImg
// @Description 查看图片位置
// @Param	url	query	string	true	"The img path"
// @Success 200 {object} query success
// @Failure 403 user not exist
// @router /img [get]
func (u *UserController) GetImg() {
	url := u.GetString("url")
	tmpUrl := url + "_tmp.jpg"
	file, _ := os.Open(url)

	img, err := jpeg.Decode(file)
	_ = file.Close()

	m := resize.Resize(255, 0, img, resize.Lanczos3)

	out, err := os.Create(tmpUrl)
	if err != nil {
		log.Fatal(err)
	}
	_ = jpeg.Encode(out, m, nil)
	_, _ = out.Seek(0, io.SeekStart)

	data, err := ioutil.ReadAll(out)
	if err != nil {
		u.Abort("GetDataFail")
	}
	_ = u.Ctx.Output.Body(data)
	_ = out.Close()
	_ = os.Remove(tmpUrl)
}

// @Title QueryVideo
// @Description 查看视频位置
// @Param	url	query	string	true	"The video path"
// @Success 200 {object} query success
// @Failure 400 user not exist
// @router /video [get]
func (u *UserController) GetVideo() {
	url := u.GetString("url")
	file, err := os.Open(url)
	if err != nil {
		u.Abort("GetDataFail")
	}

	data, err := ioutil.ReadAll(file)
	if err != nil {
		u.Abort("ReadFileFail")
	}
	_ = u.Ctx.Output.Body(data)
}

// @Title GetAllLabels
// @Description 查看所有标签
// @Success 200 {object} query success
// @Failure 403 user not exist
// @router /all/label [get]
func (u *UserController) GetAllLabels() {
	var label models.Label
	resp := make(map[string]interface{})
	filePtr, err := os.Open("data/label.json")
	if err != nil {
		u.Abort("GetDataFail")
	}

	decoder := json.NewDecoder(filePtr)
	err = decoder.Decode(&label)

	resp["msg"] = "获取所有标签成功"
	resp["data"] = label

	u.RetData(resp)
}

// @Title GetAllUniversities
// @Description 查看所有学校
// @Success 200 {object} query success
// @Failure 400 data not exists
// @router /all/university [get]
func (u *UserController) GetAllUniversities() {
	var universities []models.University
	resp := make(map[string]interface{})
	filePtr, err := os.Open("data/university.json")
	if err != nil {
		u.Abort("GetDataFail")
	}

	decoder := json.NewDecoder(filePtr)
	err = decoder.Decode(&universities)

	resp["msg"] = "获取所有学校成功"
	resp["data"] = universities

	u.RetData(resp)
}

// @Title AddUserAreaInfo
// @Description 添加用户的地区信息
// @Param	Province	formData	string	true	"省份"
// @Param	City	formData	string	true	"城市"
// @Param	Area	formData	string	true	"区域"
// @Success 200 {object}	add success
// @Failure 400 user not login
// @router /area [post]
func (u *UserController) AddUserAreaInfo() {
	var user models.User
	resp := make(map[string]interface{})
	if err := u.ParseForm(&user); err != nil {
		u.Abort("JsonToObjectFail")
	}
	user.Id = u.getUserIdFromSession()

	err := models.UpdateUserArea(user)
	if err != nil {
		u.Abort("AddDataFail")
	} else {
		resp["msg"] = "用户区域信息添加成功"
	}

	u.RetData(resp)
}

// @Title QueryFellow
// @Description 查看(筛选)我的老乡列表
// @Param	education	query	int	false	"筛选条件-学历(0-专科 1-本科 2-硕士 3-博士 4-博士后)"
// @Param	grade	query	int	false	"筛选条件-年级"
// @Param	gender	query	int	false	"筛选条件-性别"
// @Param	keyword	query	int	false	"查找的关键字"
// @Param	page	query 	int	true	"The page number(the index start with 0)"
// @Param	limit	query 	int	true	"The limit number"
// @Success 200 {object} query success
// @Failure 403 user not exist
// @router /fellow/list [get]
func (u *UserController) QueryFellow() {
	resp := make(map[string]interface{})
	keyword := u.GetString("keyword")
	userId := u.getUserIdFromSession()
	limit, page := u.getLimitAndPage()
	education, err := u.GetInt("education")
	if err != nil {
		education = -1
	}
	grade, err := u.GetInt("grade")
	if err != nil {
		grade = -1
	}
	gender, err := u.GetInt("gender")
	if err != nil {
		gender = -1
	}

	users, err := models.FilterFellow(userId, keyword, education, grade, gender, page, limit)
	if err != nil {
		u.Abort(err.Error())
	}

	resp["data"] = users
	resp["msg"] = "获取老乡列表成功"

	u.RetData(resp)
}

// @Title FilterStudentLeaders
// @Description 筛选学生干部列表
// @Param	position	query	int	false	"筛选条件-职位(0-干事 1-部长/副部长 2-会长/副会长 3-主席/副主席)"
// @Param	department	query	string	false	"筛选条件-任职部门"
// @Param	level	query	int	false	"筛选条件-职务层次"
// @Param	gender	query	int	false	"筛选条件-性别"
// @Param	keyword	query	string	false	"查找关键字"
// @Param	page	query 	int	true	"The page number(the index start with 0)"
// @Param	limit	query 	int	true	"The limit number"
// @Success 200 {object} query success
// @Failure 400 user not exist
// @router /position/list [get]
func (u *UserController) FilterStudentLeaders() {
	resp := make(map[string]interface{})
	keyword := u.GetString("keyword")
	position, err := u.GetInt("position")
	if err != nil {
		position = -1
	}
	department := u.GetString("department")
	level, err := u.GetInt("level")
	if err != nil {
		level = -1
	}
	gender, err := u.GetInt("gender")
	if err != nil {
		gender = -1
	}
	limit, page := u.getLimitAndPage()
	userId := u.getUserIdFromSession()

	users, err := models.FilterStudentLeaders(userId, keyword, position, department, level, gender, page, limit)
	if err != nil {
		u.Abort(err.Error())
	}

	resp["data"] = users
	resp["msg"] = "筛选学生干部列表成功"

	u.RetData(resp)
}

// @Title QueryCompanyInvite
// @Description 查看公司邀请
// @Param	page	query 	int	true	"The page number(the index start with 0)"
// @Param	limit	query 	int	true	"The limit number"
// @Success 200 {object} query success
// @Failure 400 user not login
// @router /invite [get]
func (u *UserController) QueryCompanyInvite() {
	resp := make(map[string]interface{})
	userId := u.getUserIdFromSession()
	limit, page := u.getLimitAndPage()

	companyInterviews, err := models.QueryCompanyInviteByUserId(userId, page, limit)
	if err != nil {
		u.Abort("GetDataFail")
	}

	resp["data"] = companyInterviews
	resp["msg"] = "获取邀请我的公司成功"

	u.RetData(resp)
}

// @Title QueryResultByUser
// @Description 用户查看 单选/双选/offer
// @Param	type	query 	int	true	"查看类型 -1: 全部 0：拒绝 1: 单选 2: 双选 3: offer"
// @Param	page	query 	int	true	"The page number(the index start with 0)"
// @Param	limit	query 	int	true	"The limit number"
// @Success 200 {object} query success
// @Failure 400 user not login
// @router /employment [get]
func (u *UserController) QueryResultByUser() {
	resp := make(map[string]interface{})
	userId := u.getUserIdFromSession()
	limit, page := u.getLimitAndPage()
	t, err := u.GetInt("type")
	if err != nil {
		u.Abort("ParamIsEmpty")
	}

	putAndInvite, err := models.QueryAllUserPutAndCompanyInviteByUser(userId, t, page, limit)
	if err != nil {
		u.Abort("GetDataFail")
	}

	resp["data"] = putAndInvite
	resp["msg"] = "获取数据成功"

	u.RetData(resp)
}

// @Title InsertResultByUser
// @Description 用户投递简历
// @Param jobId form int true "岗位id"
// @Param companyId form int true "公司id"
// @Success 200 {object} query success
// @Failure 400 user not login
// @router /employment [post]
func (u *UserController) InsertResultByUser() {
	resp := make(map[string]interface{})
	var putAndInvite models.UserPutAndCompanyInvite
	userId := u.getUserIdFromSession()
	jobId, err := u.GetInt("jobId")
	companyId, err := u.GetInt("companyId")
	if err != nil {
		u.Abort("ParamIsEmpty")
	}

	putAndInvite.Company = &models.Company{Id: companyId}
	putAndInvite.Job = &models.CompanyJob{Id: jobId}
	putAndInvite.User = &models.User{Id: userId}
	putAndInvite.UserAnswer = 1
	putAndInvite.CompanyAnswer = 2
	putAndInvite.IsPut = true
	putAndInvite.Offer = 3
	if err != nil {
		u.Abort("")
	}
	id, err := models.InsertUserPutAndCompanyInvite(putAndInvite)
	if err != nil {
		u.Abort("AddDataFail")
	}

	resp["data"] = id
	resp["msg"] = "投递简历成功"

	u.RetData(resp)
}

// @Title UpdateResultByUser
// @Description 用户更新 单选/双选/offer
// @Param jobId form int true "岗位id"
// @Param answer form int true "0: 拒绝 1: 接受"
// @Success 200 {object} query success
// @Failure 400 user not login
// @router /employment [put]
func (u *UserController) UpdateResultByUser() {
	resp := make(map[string]interface{})
	userId := u.getUserIdFromSession()
	answer, err := u.GetInt("answer")
	jobId, err := u.GetInt("jobId")
	if err != nil {
		u.Abort("ParamIsEmpty")
	}

	putAndInvite, err := models.QueryUserPutAndCompanyInvite(userId, jobId)
	if err != nil {
		u.Abort("")
	}
	if putAndInvite.Offer == 3 {
		putAndInvite.UserAnswer = answer
		err = models.UpdateUserPutAndCompanyInvite(putAndInvite, "user_answer")
	} else if putAndInvite.Offer == 2 {
		putAndInvite.Offer = answer
		err = models.UpdateUserPutAndCompanyInvite(putAndInvite, "offer")
	}
	if err != nil {
		u.Abort("GetDataFail")
	}

	resp["data"] = putAndInvite
	resp["msg"] = "更新数据成功"

	u.RetData(resp)
}

// @Title UpdateCompanyInvite
// @Description 更新公司邀请(接收或拒绝)
// @Param	Id	body	int	true	"需要更新的邀请id"
// @Param	ResultType	body	int	true	"需要更新的resultType(0-拒绝 1-接受)"
// @Success 200 {object} query success
// @Failure 400 user not login
// @router /invite [put]
func (u *UserController) UpdateCompanyInvite() {
	resp := make(map[string]interface{})
	req := make(map[string]interface{})
	var companyInterview models.CompanyInterview
	err := json.Unmarshal(u.Ctx.Input.RequestBody, &req)
	if err != nil {
		u.Abort("JsonToObjectFail")
	}
	companyInterview.Id = int(req["Id"].(float64))
	companyInterview.ResultType = int(req["ResultType"].(float64))
	userId := u.getUserIdFromSession()
	companyInterview.User, err = models.QueryUserById(userId)

	err = models.UpdateCompanyInvite(companyInterview)
	if err != nil {
		u.Abort(err.Error())
	}

	resp["msg"] = "更新坐等offer成功"

	u.RetData(resp)
}

// @Title AddSelfIntroduction
// @Description 添加自我介绍
// @Param introduction formData string true "自我介绍的video路径"
// @Param type formData int true "0: 视频 1: 音频"
// @Success 200 {object} query success
// @Failure 400 user not login
// @router /self-introduction [post]
func (u *UserController) AddSelfIntroduction() {
	var selfIntroduction models.UserSelfIntroduction
	resp := make(map[string]interface{})
	selfIntroduction.Video = u.GetString("introduction")
	selfIntroduction.Type, _ = u.GetInt("type")
	userId := u.getUserIdFromSession()

	user, _ := models.QueryUserById(userId)
	selfIntroduction.User = user

	id, err := models.AddSelfIntroduction(selfIntroduction)
	if err != nil {
		u.Abort("AddDataFail")
	} else {
		resp["msg"] = "添加自我介绍成功"
		resp["SelfIntroductionId"] = id
	}

	u.RetData(resp)
}

// @Title QuerySelfIntroduction
// @Description 查看自我介绍
// @Param type query int true "0: 视频 1: 音频"
// @Success 200 {object} query success
// @Failure 400 user not login
// @router /self-introduction [get]
func (u *UserController) QuerySelfIntroduction() {
	resp := make(map[string]interface{})
	userId := u.getUserIdFromSession()
	t, err := u.GetInt("type")
	if err != nil {
		u.Abort("ParamIsEmpty")
	}

	selfIntroduction, err := models.QuerySelfIntroduction(userId, t)
	if err != nil {
		u.Abort(err.Error())
	}

	resp["data"] = selfIntroduction
	resp["msg"] = "获取自我介绍成功"

	u.RetData(resp)
}

// @Title AddUserSchool
// @Description	添加用户信息(小学，初中，高中)
// @Param	HighSchool	formData	string	true	"高中学校"
// @Param	JuniorHighSchool	formData	string	true	"初中学校"
// @Param	PrimarySchool	formData	string	true	"小学"
// @Success 200 {object} query success
// @Failure 400 user not login
// @router /school [post]
func (u *UserController) AddUserSchool() {
	var user models.User
	resp := make(map[string]interface{})
	if err := u.ParseForm(&user); err != nil {
		u.Abort("JsonToObjectFail")
	}

	user.Id = u.getUserIdFromSession()

	err := models.UpdateUserSchool(user)
	if err != nil {
		u.Abort(err.Error())
	} else {
		resp["msg"] = "添加用户信息成功"
	}

	u.RetData(resp)
}

// @Title AddUserPositionInfo
// @Description 添加用户的职位信息
// @Param	Department	formData	string	true	"任职部门"
// @Param	WorkTimeStartYear	formData	int	true	"任职期限-开始-年"
// @Param	WorkTimeStartMonth	formData	int	true	"任职期限-开始-月"
// @Param	WorkTimeEndYear	formData	int	true	"任职期限-结束-年"
// @Param	WorkTimeEndMonth	formData	int	true	"任职期限-结束-月"
// @Param	Level	formData	string	true	"任职层级"
// @Param	Position	formData	string	true	"任职职位"
// @Success 200 {object}	add success
// @Failure 400 user not login
// @router /position [post]
func (u *UserController) AddUserPositionInfo() {
	var user models.User
	resp := make(map[string]interface{})
	if err := u.ParseForm(&user); err != nil {
		u.Abort("JsonToObjectFail")
	}
	user.Id = u.getUserIdFromSession()

	err := models.UpdateUserPosition(user)
	if err != nil {
		u.Abort("AddDataFail")
	} else {
		resp["msg"] = "用户职位添加成功"
	}

	u.RetData(resp)
}

// @Title QueryUserSchool
// @Description 查看我的学校
// @Param	id	query	int	false	"查看他人学校时可添加该参数"
// @Success 200 {object} query success
// @Failure 400 user not login
// @router /school [get]
func (u *UserController) QueryUserSchool() {
	resp := make(map[string]interface{})
	var user *models.User
	id, err := u.GetInt("id")
	userId := u.getUserIdFromSession()

	if id == userId || err != nil {
		user, err = models.QueryUserById(userId)
	} else {
		user, err = models.QueryUserById(id)
	}
	if err != nil {
		u.Abort("UserNotExist")
	}

	data := make(map[string]interface{})
	data["PrimarySchool"] = user.PrimarySchool
	data["JuniorHighSchool"] = user.JuniorHighSchool
	data["HighSchool"] = user.HighSchool

	resp["data"] = data
	resp["msg"] = "获取我的学校成功"

	u.RetData(resp)
}

// @Title FilterUserSchool
// @Description 筛选用户学校
// @Param	city	query	string	false	"筛选条件-城市"
// @Param	gender	query	int	false	"筛选条件-性别"
// @Param	type	query 	int	true	"筛选条件-校友类型(1-小学校友 1-初中校友 2-高中校友)"
// @Param	keyword	query	string	false	"查找的关键字"
// @Param	page	query 	int	true	"The page number(the index start with 0)"
// @Param	limit	query 	int	true	"The limit number"
// @Success 200 {object} query success
// @Failure 400 user not exist
// @router /school/list [get]
func (u *UserController) FilterUserSchool() {
	resp := make(map[string]interface{})
	keyword := u.GetString("keyword")
	city := u.GetString("city")
	gender, err := u.GetInt("gender")
	if err != nil {
		gender = -1
	}
	t, err := u.GetInt("type")
	if err != nil {
		t = -1
	}
	limit, page := u.getLimitAndPage()
	userId := u.getUserIdFromSession()

	users, err := models.FilterUserSchool(userId, keyword, city, gender, t, page, limit)
	if err != nil {
		u.Abort(err.Error())
	}

	resp["data"] = users
	resp["msg"] = "筛选校友列表成功"

	u.RetData(resp)
}

// @Title AddUserHobby
// @Description 添加用户的兴趣爱好信息
// @Param	Hobby1	formData	string	true	"兴趣爱好1"
// @Param	Hobby2	formData	string	true	"兴趣爱好2"
// @Param	Hobby3	formData	string	true	"兴趣爱好3"
// @Success 200 {object}	add success
// @Failure 400 user not login
// @router /hobby [post]
func (u *UserController) AddUserHobby() {
	var user models.User
	resp := make(map[string]interface{})
	if err := u.ParseForm(&user); err != nil {
		u.Abort("JsonToObjectFail")
	}
	user.Id = u.getUserIdFromSession()

	err := models.UpdateUserHobby(user)
	if err != nil {
		u.Abort("AddDataFail")
	} else {
		resp["msg"] = "用户兴趣爱好添加成功"
	}

	u.RetData(resp)
}

// @Title FilterUserHobby
// @Description 筛选用户兴趣爱好
// @Param	education	query	int	false	"筛选条件-学历"
// @Param	grade	query	int	false	"筛选条件-年级"
// @Param	gender	query	int	false	"筛选条件-性别"
// @Param	hobby	query	int	false	"筛选条件-爱好"
// @Param	page	query 	int	true	"The page number(the index start with 0)"
// @Param	limit	query 	int	true	"The limit number"
// @Success 200 {object} query success
// @Failure 400 user not exist
// @router /hobby/list [get]
func (u *UserController) FilterUserHobby() {
	resp := make(map[string]interface{})
	keyword := u.GetString("keyword")
	hobby := u.GetString("hobby")
	education, err := u.GetInt("education")
	if err != nil {
		education = -1
	}
	grade, err := u.GetInt("level")
	if err != nil {
		grade = -1
	}
	gender, err := u.GetInt("gender")
	if err != nil {
		gender = -1
	}
	limit, page := u.getLimitAndPage()
	userId := u.getUserIdFromSession()

	users, err := models.FilterHobby(userId, keyword, hobby, education, grade, gender, page, limit)
	if err != nil {
		u.Abort(err.Error())
	}

	resp["data"] = users
	resp["msg"] = "筛选兴趣爱好列表成功"

	u.RetData(resp)
}

// @Title QueryUserHobby
// @Description 查看我的爱好
// @Param	id	query	int	false	"查看他人爱好时可添加该参数"
// @Success 200 {object} query success
// @Failure 400 user not login
// @router /hobby [get]
func (u *UserController) QueryUserHobby() {
	resp := make(map[string]interface{})
	var user *models.User
	id, err := u.GetInt("id")
	userId := u.getUserIdFromSession()

	if id == userId || err != nil {
		user, err = models.QueryUserById(userId)
	} else {
		user, err = models.QueryUserById(id)
	}
	if err != nil {
		u.Abort("GetDataFail")
	}

	data := make(map[string]interface{})
	data["hobby1"] = user.Hobby1
	data["hobby2"] = user.Hobby2
	data["hobby3"] = user.Hobby3

	resp["data"] = data
	resp["msg"] = "获取我的兴趣爱好成功"

	u.RetData(resp)
}

// @Title logout
// @Description Logs out current logged in user session
// @Success 200 {string} logout success
// @router /logout [get]
func (u *UserController) Logout() {
	u.DelSession("userId")
	resp := make(map[string]interface{})
	resp["msg"] = "退出成功"

	u.RetData(resp)
}

// @Title importUser
// @Description 导入用户
// @Param	file	formData	file	true	"用户excel"
// @Success 200 {string} import success
// @router /import [post]
func (u *UserController) ImportUser() {
	var users []models.User
	resp := make(map[string]interface{})
	path := "data/user/" + utils.GenerateRandomFileName()
	err := u.SaveToFile("file", path)
	if err != nil {
		u.Abort("SystemFailed")
	}

	f, err := excelize.OpenFile(path)
	if err != nil {
		println(err.Error())
		return
	}
	rows, err := f.GetRows(f.GetSheetName(1))
	for i, row := range rows {
		if len(row) < 10 || i == 0 || row[1] == "" {
			continue
		}
		grade, _ := strconv.Atoi(row[10])
		user := models.User{
			Nickname:   row[1],
			Gender:     str2gender(row[2]),
			Area:       row[4],
			Telephone:  row[5],
			Password:   row[5],
			University: row[8],
			Major:      row[9],
			Grade:      grade,
		}
		users = append(users, user)
	}
	num, err := models.ImportUser(users)
	if err != nil {
		u.Abort(err.Error())
	}

	resp["msg"] = "导入成功"
	resp["data"] = num

	u.RetData(resp)
}

// @Title UpdateUserElite
// @Description 更新用户精英值
// @Param userId int true "用户id"
// @Success 200 {object} update success
// @Failure 400 user not login
// @router /elite [put]
func (u *UserController) UpdateUserElite() {
	resp := make(map[string]interface{})
	userId, err := u.GetInt("userId")
	if err != nil {
		u.Abort("ParamIsEmpty")
	}

	err = models.UpdateUserElite(userId)
	if err != nil {
		u.Abort(err.Error())
	}

	resp["msg"] = "更新用户精英值成功"

	u.RetData(resp)
}

// @Title GenerateResumePDF
// @Description 下载我的简历
// @Success 200 {object} download success
// @Failure 400 user not login
// @router /resume/download [get]
func (u *UserController) GenerateResumePDF() {
	resp := make(map[string]interface{})

	userId := u.getUserIdFromSession()
	resumeId := userId

	user, err := models.QueryUserById(userId)
	resume, err := models.QueryResumeByResumeId(resumeId)
	labels, err := models.QueryUserLabel(userId)
	honors, err := models.QueryUserHonor(userId, 0, 100)
	if err != nil {
		u.Abort(err.Error())
	}
	gender := "保密"
	if user.Gender == 1 {
		gender = "男"
	} else if user.Gender == 2 {
		gender = "女"
	}

	filename := utils.GenerateRandomFileName()
	pdf := gopdf.GoPdf{}
	pdf.SetMargins(100, 50, 100, 50)
	pdf.Start(gopdf.Config{PageSize: *gopdf.PageSizeA4})
	pdf.AddPage()
	err = pdf.AddTTFFont("Yahei", "utils/font/Yahei.ttf")
	if err != nil {
		log.Print(err.Error())
		return
	}
	err = pdf.SetFont("Yahei", "", 14)
	if err != nil {
		log.Print(err.Error())
		return
	}
	pdf.SetStrokeColor(217, 217, 217)

	pdf.SetX(50)
	pdf.SetY(50)
	_ = pdf.Cell(nil, "ID: "+strconv.Itoa(user.Id))
	pdf.SetX(450)
	_ = pdf.Cell(nil, "【校招云】")

	pdf.SetLineWidth(3)
	pdf.Line(50, 70, 520, 70)
	pdf.Br(30)

	pdf.SetX(50)
	_ = pdf.Cell(nil, user.RealName)
	pdf.Br(30)

	_ = pdf.SetFont("Yahei", "", 10)
	pdf.SetX(50)
	_ = pdf.Cell(nil, "性别: "+gender)
	pdf.Br(15)

	pdf.SetX(50)
	_ = pdf.Cell(nil, "民族: "+"汉")
	pdf.Br(15)

	pdf.SetX(50)
	_ = pdf.Cell(nil, "出生年月: "+user.CreateTime.String())
	pdf.Br(15)

	pdf.SetX(50)
	_ = pdf.Cell(nil, "家住城市: "+user.Province+user.City)
	pdf.Br(15)

	pdf.SetX(50)
	_ = pdf.Cell(nil, "政治面貌: "+user.City)
	pdf.Br(18)

	_ = pdf.SetFont("Yahei", "", 14)
	pdf.SetLineWidth(20)
	pdf.Line(50, pdf.GetY()+7, 520, pdf.GetY()+7)
	pdf.SetX(50)
	_ = pdf.Cell(nil, "求职要求")
	pdf.Br(25)
	_ = pdf.SetFont("Yahei", "", 10)

	pdf.SetX(50)
	_ = pdf.Cell(nil, "◆意向岗位: "+resume.TargetJob)
	pdf.Br(15)

	pdf.SetX(50)
	_ = pdf.Cell(nil, "◆目标城市: "+resume.TargetCity)
	pdf.Br(15)

	pdf.SetX(50)
	_ = pdf.Cell(nil, "◆最低月薪: "+strconv.Itoa(resume.TargetSalary))
	pdf.Br(15)

	_ = pdf.SetFont("Yahei", "", 14)
	pdf.SetLineWidth(20)
	pdf.Line(50, pdf.GetY()+7, 520, pdf.GetY()+7)
	pdf.SetX(50)
	_ = pdf.Cell(nil, "教育经历")
	pdf.Br(25)
	_ = pdf.SetFont("Yahei", "", 10)

	pdf.SetX(50)
	_ = pdf.Cell(nil, "◆最高学历: "+"")
	pdf.SetX(200)
	_ = pdf.Cell(nil, "◆次高学历: "+"")
	pdf.Br(15)

	pdf.SetX(50)
	_ = pdf.Cell(nil, "◆学校名称: "+user.University)
	pdf.SetX(200)
	_ = pdf.Cell(nil, "◆学校名称: "+user.University)
	pdf.Br(15)

	pdf.SetX(50)
	_ = pdf.Cell(nil, "◆专业: "+user.Major)
	pdf.SetX(200)
	_ = pdf.Cell(nil, "◆专业: "+user.Major)
	pdf.Br(15)

	pdf.SetX(50)
	_ = pdf.Cell(nil, "◆外语计算机: "+resume.ForeignLanguage+resume.OtherComputerInformation)
	pdf.Br(15)

	pdf.SetX(50)
	_ = pdf.Cell(nil, "◆主修课程: "+resume.Major)
	pdf.Br(15)

	_ = pdf.SetFont("Yahei", "", 14)
	pdf.SetLineWidth(20)
	pdf.Line(50, 365, 520, 365)
	pdf.SetX(50)
	_ = pdf.Cell(nil, "学生工作")
	pdf.Br(20)
	_ = pdf.SetFont("Yahei", "", 10)

	pdf.SetX(50)
	_ = pdf.Cell(nil, "◆2019.09-2020.07 浙江大学研究生院学生会主席")
	pdf.Br(15)

	pdf.SetX(50)
	_ = pdf.Cell(nil, " 主要工作:沟潭之水，凝滞沉闷;飞瀑之流，奋迅高亢。同是为水，性却异，前者满足安逸，后者进去不已。")
	pdf.Br(15)
	pdf.SetX(50)
	_ = pdf.Cell(nil, "点点滴滴，有过激情，有过惊喜，亦然有过委屈，有过失望。")
	pdf.Br(15)

	pdf.SetX(50)
	_ = pdf.Cell(nil, "◆2019.09-2020.07 浙江大学研究生院学生会主席")
	pdf.Br(15)

	pdf.SetX(50)
	_ = pdf.Cell(nil, " 主要工作:沟潭之水，凝滞沉闷;飞瀑之流，奋迅高亢。同是为水，性却异，前者满足安逸，后者进去不已。")
	pdf.Br(15)
	pdf.SetX(50)
	_ = pdf.Cell(nil, "点点滴滴，有过激情，有过惊喜，亦然有过委屈，有过失望。但最重要的是收获经验.")
	pdf.Br(15)

	pdf.SetX(50)
	_ = pdf.Cell(nil, "◆2019.09-2020.07 浙江大学研究生院学生会主席")
	pdf.Br(25)

	_ = pdf.SetFont("Yahei", "", 14)
	pdf.SetLineWidth(20)
	pdf.Line(50, pdf.GetY()+7, 520, pdf.GetY()+7)
	pdf.SetX(50)
	_ = pdf.Cell(nil, "社会实践")
	pdf.Br(20)
	_ = pdf.SetFont("Yahei", "", 10)

	if len(resume.SocialPractices) < 3 {
		u.Abort("SocialPracticeNotEnough")
	}

	for i, socialPractice := range resume.SocialPractices {
		pdf.SetX(50)
		_ = pdf.Cell(nil, "◆"+strconv.Itoa(socialPractice.Year)+"/"+strconv.Itoa(socialPractice.Month)+"/"+strconv.Itoa(socialPractice.Day)+" "+socialPractice.Content)
		pdf.Br(15)

		if i == 3 {
			break
		}
	}

	_ = pdf.SetFont("Yahei", "", 14)
	pdf.SetLineWidth(20)
	pdf.Line(50, pdf.GetY()+7, 520, pdf.GetY()+7)
	pdf.SetX(50)
	_ = pdf.Cell(nil, "获奖情况")
	pdf.Br(20)
	_ = pdf.SetFont("Yahei", "", 10)

	for i, honor := range honors {
		pdf.SetX(50)
		_ = pdf.Cell(nil, "◆"+strconv.Itoa(honor.GetTimeYear)+"/"+strconv.Itoa(honor.GetTimeMonth)+" "+utils.Num2HonorType(honor.Type)+" ("+honor.Level+")"+"  "+honor.Description)
		pdf.Br(15)

		if i == 5 {
			break
		}
	}

	_ = pdf.SetFont("Yahei", "", 14)
	pdf.SetLineWidth(20)
	pdf.Line(50, pdf.GetY()+7, 520, pdf.GetY()+7)
	pdf.SetX(50)
	_ = pdf.Cell(nil, "标签与评价")
	pdf.Br(25)
	_ = pdf.SetFont("Yahei", "", 10)

	pdf.SetX(50)
	var character string
	var ability string
	var hobby string
	for _, s := range labels {
		if s.Type == 0 {
			character += s.Label + " "
		} else if s.Type == 1 {
			ability += s.Label + " "
		} else if s.Type == 2 {
			hobby += s.Label + " "
		}
	}
	_ = pdf.Cell(nil, "形象性格: "+character)
	pdf.Br(15)
	pdf.SetX(50)
	_ = pdf.Cell(nil, "核心能力: "+ability)
	pdf.Br(15)
	pdf.SetX(50)
	_ = pdf.Cell(nil, "兴趣特长: "+hobby)
	pdf.Br(15)

	err = pdf.Image("data/avatar/xxx.jpg", 430, 80, &gopdf.Rect{W: 85, H: 95})

	err = pdf.WritePdf("data/resume/" + filename)
	if err != nil {
		print(err.Error())
		u.Abort("GetDataFail")
	}

	u.Ctx.Output.Download("data/resume/"+filename, "resume.pdf")
	u.RetData(resp)
}

// @Title GetAllJobs
// @Description 查看所有岗位(分三级)
// @Success 200 {object} query success
// @Failure 403 user not exist
// @router /all/job [get]
func (u *UserController) GetAllJobs() {
	var jobs []models.SecondJob
	resp := make(map[string]interface{})
	filePtr, err := os.Open("data/job.json")
	if err != nil {
		u.Abort("GetDataFail")
	}

	decoder := json.NewDecoder(filePtr)
	err = decoder.Decode(&jobs)

	resp["msg"] = "获取所有岗位成功"
	resp["data"] = jobs

	u.RetData(resp)
}

// @Title GetAllStarCompanies
// @Description 查看所有关注的公司
// @Param	page	query 	int	true	"The page number(the index start with 0)"
// @Param	limit	query 	int	true	"The limit number"
// @Success 200 {object} query success
// @Failure 403 user not exist
// @router /company/all/stars [get]
func (u *UserController) GetAllStarCompanies() {
	resp := make(map[string]interface{})
	userId := u.getUserIdFromSession()
	limit, page := u.getLimitAndPage()

	starCompanies, err := models.QueryAllStarCompanies(userId, page, limit)
	if err != nil {
		u.Abort("GetDataFail")
	}

	resp["msg"] = "获取所有关注公司成功"
	resp["data"] = starCompanies

	u.RetData(resp)
}

// @Title StarCompany
// @Description 关注指定公司
// @Param companyId form int true "公司id"
// @Success 200 {int} star success
// @Failure 403 user not exist
// @router /company/star/:companyId [post]
func (u *UserController) StarCompany() {
	resp := make(map[string]interface{})
	userId := u.getUserIdFromSession()
	companyId, err := u.GetInt(":companyId")
	if err != nil {
		u.Abort("ParamIsEmpty")
	}

	var userStarCompany models.UserStarCompany
	userStarCompany.User = &models.User{Id: userId}
	userStarCompany.Company = &models.Company{Id: companyId}
	starId, err := models.StarCompany(&userStarCompany)
	if err != nil {
		u.Abort("AddDataFail")
	}

	resp["msg"] = "关注公司成功"
	resp["data"] = starId

	u.RetData(resp)
}

// @Title CancelStarCompany
// @Description 取消关注指定公司
// @Param companyId form int true "公司id"
// @Success 200 {int} star success
// @Failure 403 user not exist
// @router /company/star/:companyId [delete]
func (u *UserController) CancelStarCompany() {
	resp := make(map[string]interface{})
	userId := u.getUserIdFromSession()
	companyId, err := u.GetInt(":companyId")
	if err != nil {
		u.Abort("ParamIsEmpty")
	}

	var userStarCompany models.UserStarCompany
	userStarCompany.User = &models.User{Id: userId}
	userStarCompany.Company = &models.Company{Id: companyId}
	err = models.CancelStarCompany(userStarCompany)
	if err != nil {
		u.Abort("DeleteDataFail")
	}

	resp["msg"] = "取消关注公司成功"

	u.RetData(resp)
}

func (u *UserController) getUserIdFromSession() int {
	id := u.GetSession("userId")
	if id == nil {
		u.Abort("UserNotLogin")
	}
	userId := id.(int)

	return userId
}

func (u *UserController) getLimitAndPage() (limit int, page int) {
	page, errPage := u.GetInt("page")
	limit, errLimit := u.GetInt("limit")
	if errLimit != nil || errPage != nil {
		u.Abort("ParamIsEmpty")
	}
	if page < 0 || limit < 0 {
		u.Abort("ReadParam")
	}

	return limit, page * limit
}

func (u *UserController) str2time(t string) time.Time {
	loc, _ := time.LoadLocation("Local")
	theTime, err := time.ParseInLocation("2006-01-02 15:04:05", t, loc)

	if err != nil {
		u.Abort("JsonToObject")
	}
	return theTime
}

func str2gender(str string) int {
	if str == "男" {
		return 1
	} else if str == "女" {
		return 2
	} else {
		return 0
	}
}
